<template>
	<view class="pagebody">
		<view class="topbox">
			<view class="justify-between align-center combody">
				<image @click="tohome" src="../static/images/header/logo.png" style="width: 162rpx;height: 50rpx;">
				</image>
				<view class="bgfff searchbox justify-between align-center">
					<input v-model="keywords" class="searchinput" type="text" placeholder="搜索关键词"
						:placeholderStyle="placeholderStyle" />
					<image @click="saveSearch" src="../static/images/home/searchimg.png"
						style="width:48rpx ;height:48rpx ;">
					</image>
				</view>
				<image @click="tonav" src="../static/images/header/nav.png" style="width:48rpx ;height:48rpx ;"></image>
			</view>
		</view>
		<view class="oldbox">
			<view style="font-weight: bold;font-size: 32rpx;color: #303B39;" class="">
				最近搜索记录
			</view>
			<view v-if="historyList.length>0" class=" hisbox">
				<view @click="tosearch(item)" class="hisitem" v-for="(item,index) in historyList" :key="index">
					{{item.text}}<image @click.stop="removeItem(index)" src="../static/images/close.png" style="width:30rpx ;height:30rpx ;margin-left: 16rpx;"></image>
				</view>
				
			</view>
			<view v-else class="hisbox">
				<view class="hisitem">
					暂无历史搜索
				</view>
			</view>

		</view>

	</view>
</template>

<script>
	import {
		getDoctorById,
		getHospitalList,
		selectNewsList
	} from '@/api/api.js';
	export default {
		data() {
			return {
				placeholderStyle: "color:#7E8987;font-size:32rpx;font-weight: 400;",
				keywords: '',
				historyList: [],
			}
		},
		onLoad() {
			this.loadHistory();
		},
		methods: {
			tohome() {
				uni.navigateTo({
					url: '/pages/home'
				})
			},
			tosearch(item) {
				uni.navigateTo({
					url: '/pages/knowledge/druglist?keywords=' + item.text
				})
			},
			// 存储
			saveSearch() {
				if (!this.keywords.trim()) {
					uni.showToast({
						title: '请输入搜索内容',
						icon: 'none'
					});
					return;
				}
				// 检查是否已存在相同记录
				const index = this.historyList.findIndex(item => item.text === this.keywords);
				if (index !== -1) {
					// 如果已存在，先移除
					this.historyList.splice(index, 1);
				}
				// 添加到数组开头
				this.historyList.unshift({
					id: Date.now(),
					text: this.keywords,
					time: new Date().getTime()
				});
				// 如果记录超过10条，移除最早的
				if (this.historyList.length > 10) {
					this.historyList.pop();
				}
				// 保存到本地存储
				uni.setStorageSync('search-history', this.historyList);

				uni.navigateTo({
					url: '/pages/knowledge/druglist?keywords=' + this.keywords
				})
			},
			// 加载历史
			loadHistory() {
				const history = uni.getStorageSync('search-history');
				console.log(history)
				if (history) {
					this.historyList = history;
				}
			},
			removeItem(index) {
				this.historyList.splice(index, 1);
				uni.setStorageSync('search-history', this.historyList);
				uni.showToast({
					title: '已删除',
					icon: 'success'
				});
			},
			tonav() {
				uni.navigateTo({
					url: '/pages/navpage'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pagebody {
		/* padding-bottom: 136rpx; */
		background: #FFF;

		.topbox {
			padding: 20rpx 24rpx;
			background: linear-gradient(180deg, rgba(0, 217, 178, 0.25) 0%, rgba(0, 217, 178, 0) 100%);

			.combody {
				background: transparent;

				.searchbox {
					width: 461rpx;
					height: 60rpx;
					background: #FFFFFF;
					border-radius: 60rpx;
					border: 2rpx solid rgba(0, 217, 178, 0.2);
					padding: 0 24rpx;

					.searchinput {
						font-size: 28rpx
					}

				}
			}
		}

		.oldbox {
			padding: 60rpx 24rpx;
		}
		.hisbox{
			margin-top: 32rpx;
			display: flex;
			justify-content: start;
			flex-wrap: wrap;
			.hisitem{
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #303B39;
				height: 62rpx;
				padding: 0 24rpx;
				background: #F0F6F5;
				border-radius:48rpx;
				margin-right: 16rpx;
				margin-bottom: 24rpx;
			}
		}
	}
</style>